package com.douhulu.bytedance.open.util.json;

import com.douhulu.bytedance.open.bean.ByteOpenAccessToken;
import com.douhulu.bytedance.open.bean.ByteOpenCallback;
import com.douhulu.bytedance.open.bean.ByteOpenClientToken;
import com.douhulu.bytedance.open.bean.ByteOpenRefreshToken;
import com.douhulu.bytedance.open.bean.result.ByteOpenAuthorizationInfo;
import com.douhulu.bytedance.open.bean.result.auth.ByteOpenAuthorizerInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * <h2> 奕泽云 -  </h2>
 *
 * @Author: 奕泽云 - 曾庆雷
 * @Email: 75881401@qq.com
 * @Since: 2022/5/1  20:43
 * CopyRight：山西威斯汀文化传播有限公司
 */
@Slf4j
public class ByteOpenGsonBuilder {

    private static final GsonBuilder INSTANCE = new GsonBuilder();
    private static volatile Gson GSON_INSTANCE;

    static {
        INSTANCE.disableHtmlEscaping();
        INSTANCE.registerTypeAdapter(ByteOpenAccessToken.class, new ByteOpenAccessTokenGsonAdapter());
        INSTANCE.registerTypeAdapter(ByteOpenRefreshToken.class, new ByteOpenRefreshTokenGsonAdapter());
        INSTANCE.registerTypeAdapter(ByteOpenAuthorizationInfo.class, new ByteOpenAuthorizationInfoGsonAdapter());
        INSTANCE.registerTypeAdapter(ByteOpenAuthorizerInfo.class, new ByteOpenAuthorizerInfoGsonAdapter());
        INSTANCE.registerTypeAdapter(ByteOpenClientToken.class, new ByteOpenClientTokenGsonAdapter());
        INSTANCE.registerTypeAdapter(ByteOpenCallback.class, new ByteOpenCallbackGsonAdapter());

    }

    public static Gson create() {

        if (Objects.isNull(GSON_INSTANCE)) {
            synchronized (INSTANCE) {
                if (Objects.isNull(GSON_INSTANCE)) {
                    GSON_INSTANCE = INSTANCE.create();
                }
            }
        }

        return GSON_INSTANCE;
    }
}
